package com.wanxi.springboot.team.manage.system.controller;


import com.wanxi.springboot.team.manage.system.api.CommonResult;
import com.wanxi.springboot.team.manage.system.model.User;
import com.wanxi.springboot.team.manage.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 蒋帆
 * @since 2020-12-21
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户控制类")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${jwt.tokenHead}")
    String tokenHead;

    @GetMapping("/getUserByName")
    @ApiOperation("根据用户编号查找用户")
    @ApiImplicitParam(name = "username",value = "用户名",required = true,paramType = "String",dataType = "json")
    public CommonResult getUserByName(@RequestParam String username) {
        return userService.getUserByName(username);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    @ApiImplicitParam(name = "user",value = "User实体类",required = true,paramType = "User",dataType = "json")
    public CommonResult login(@RequestBody User user) {
        return userService.login(user);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    @ApiImplicitParam(name = "user",value = "User实体类",required = true,paramType = "User",dataType = "json")
    public CommonResult register(@RequestBody User user) {
        return userService.register(user);
    }

    @PreAuthorize("hasAnyAuthority('user:get')")
    @GetMapping("/getUsers")
    @ApiOperation("条件查询获取用户列表")
    @ApiImplicitParam(name = "map",value = "带有page,limit,param,入学时间范围等条件的map集合",required = true,paramType = "Map",dataType = "json")
    public CommonResult getUsers(@RequestParam Map map) {
        return userService.getUsers(map);
    }

    @PreAuthorize("hasAnyAuthority('user:delete')")
    @GetMapping("/deleteUser")
    @ApiOperation("删除用户")
    @ApiImplicitParam(name = "code",value = "用户主键",required = true,paramType = "String",dataType = "json")
    public CommonResult deleteUser(@RequestParam String code){
        return userService.deleteUser(code);
    }

//    @PreAuthorize("hasAnyAuthority('user:update')")
    @PostMapping("/updateUser")
    @ApiOperation("修改用户")
    @ApiImplicitParam(name = "user",value = "用户实体类",required = true,paramType = "User",dataType = "json")
    public CommonResult updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    @GetMapping("/getCodeList")
    @ApiOperation("获取码表")
    public CommonResult getCodeList(){
        return userService.getCodeList();
    }

    @PreAuthorize("hasAnyAuthority('user:status:update')")
    @PostMapping("/updateStatus")
    @ApiOperation("更新用户状态")
    public CommonResult updateStatus(@RequestParam("code") String code, @RequestParam("status") String status){
        return userService.updateStatus(code,status);
    }

    @GetMapping("/getLeaderList")
    @ApiOperation("获取组长列表")
    public CommonResult getLeaderList(){
        return userService.getLeaderList();
    }

    @GetMapping("/getCoachList")
    @ApiOperation("获取教练列表")
    public CommonResult getCoachList(){
        return userService.getCoachList();
    }

    /**
     * 小组成员管理
     * @param teamId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('user:get:team:user:by:team')")
    @GetMapping("/getTeamUser")
    @ApiOperation("通过小组id获取小组成员与未分组成员")
    @ApiImplicitParam(name = "teamId",value = "小组id",required = true,paramType = "Integer",dataType = "json")
    public CommonResult getTeamUser(@RequestParam Integer teamId){
        return userService.getTeamUser(teamId);
    }

    @PreAuthorize("hasAnyAuthority('user:get:team:member:by:team')")
    @GetMapping("/getTeamMember")
    @ApiOperation("通过小组id获取小组成员")
    @ApiImplicitParam(name = "teamId",value = "小组id",required = true,paramType = "Integer",dataType = "json")
    public CommonResult getTeamMember(@RequestParam Integer teamId){
        return userService.getTeamMember(teamId);
    }

    /**
     * 签到管理
     * @param userId
     * @return
     */
    @PreAuthorize("hasAnyAuthority('user:get:team:member:by:user')")
    @GetMapping("/getTeamMemberByUser")
    @ApiOperation("通过用户id获取用户所在小组以及成员信息")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "Integer",dataType = "json")
    public CommonResult getTeamMemberByUser(@RequestParam Integer userId){
        return userService.getTeamMemberByUser(userId);
    }
    @PreAuthorize("hasAnyAuthority('user:no:sign:team:member')")
    @GetMapping("/getNoSignTeamMemberByUser")
    @ApiOperation("通过用户id获取用户所在小组以及今日未打卡成员信息")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,paramType = "Integer",dataType = "json")
    public CommonResult getNoSignTeamMemberByUser(@RequestParam Integer userId){
        return userService.getNoSignTeamMemberByUser(userId);
    }

}
